#!/usr/bin/env bash
set -ex

cd $(dirname $0)/..

source ./scripts/version.sh

DOCKER_BUILDKIT=${DOCKER_BUILDKIT:-1} docker image build \
    --build-arg TAG=${VERSION} \
    --build-arg KUBERNETES_VERSION=${KUBERNETES_VERSION} \
    --build-arg MAJOR=${VERSION_MAJOR} \
    --build-arg MINOR=${VERSION_MINOR} \
    --build-arg DAPPER_HOST_ARCH=${GOARCH} \
    --build-arg CACHEBUST="$(date +%s%N)" \
    --tag ${REPO}/${PROG}-runtime:${DOCKERIZED_VERSION} \
    --tag ${REPO}/${PROG}-runtime:${DOCKERIZED_VERSION}-${GOOS}-${GOARCH} \
    --target runtime \
    --file Dockerfile \
    .

if [ "${GOARCH}" != "s390x" ] && [ "${GOARCH}" != "arm64" ] && [ -z "$SKIP_WINDOWS" ]; then
  DOCKER_BUILDKIT=${DOCKER_BUILDKIT:-1} docker image build \
      --build-arg TAG=${VERSION} \
      --build-arg KUBERNETES_VERSION=${KUBERNETES_VERSION} \
      --build-arg MAJOR=${VERSION_MAJOR} \
      --build-arg MINOR=${VERSION_MINOR} \
      --build-arg CACHEBUST="$(date +%s%N)" \
      --tag ${REPO}/${PROG}-runtime:${DOCKERIZED_VERSION}-windows-amd64 \
      --target windows-runtime \
      --file Dockerfile.windows \
      .
  # Only ever used in its compressed form for e2e tests
  mkdir -p build/images
  docker image save \
    ${REPO}/${PROG}-runtime:${DOCKERIZED_VERSION}-windows-${GOARCH} | \
    zstd -T0 -16 -f --long=25 --no-progress - -o build/images/${PROG}-images.windows-${GOARCH}.tar.zst    
fi
mkdir -p build/images
docker image save \
    --output build/images/${PROG}-runtime.tar \
    ${REPO}/${PROG}-runtime:${DOCKERIZED_VERSION}-${GOOS}-${GOARCH}
